# -*- coding:utf-8 -*-
import time

from spider.common.util.DataUtil import DataUtil
from spider.common.util.HtmlUtil import HtmlUtil
from spider.common.util.SQLUtil import SafeMySQLCRUD
from spider.common.util.SeleniumUtils import SeleniumUtils


def trylogin(seleniumUtils):
    url='https://99vv81.com/online_info.php?id=51016'
    seleniumUtils.open_url(URL=url)
    seleniumUtils.move_and_clink_xpath(xpath='//div[@class="user-btns"]//a',fast_falg=True)
    userName='1781216420@qq.com'
    password='15936477205'
    code='1234'
    seleniumUtils.input_text(xpath='//input[@name="username"]',text=userName)
    seleniumUtils.input_text(xpath='//input[@name="pass"]',text=password)
    seleniumUtils.input_text(xpath='//input[@name="code"]',text=code)
    #点击登录
    # seleniumUtils.move_and_clink_xpath(xpath='//input[@class="submit"]')



def loadTel(seleniumUtils):
    seleniumUtils.scoll_low()
    print("滑动到浏览器底部")
    #获取联系方式
    xpath='//div[@class="info-btn getContact"]//a'
    seleniumUtils.move_and_clink_xpath(xpath=xpath,fast_falg=True)
    return seleniumUtils.source_code()



if __name__ == "__main__":
    chrome_path = r"D:\software\chrome\chrome-win64\chrome.exe"  # 替换为你的Chrome路径

    sqlUtil = SafeMySQLCRUD()
    datas = sqlUtil.retrieve_jsons(table='list')
    dataUtil=DataUtil()
    htmlUtil = HtmlUtil()
    seleniumUtils = SeleniumUtils(chrome_path=chrome_path, headless_flag=False)

    groupDatas=dataUtil.numpy_split(datas,1)
    trylogin(seleniumUtils)
    time.sleep(20)

    for groupData in groupDatas:
        saveDatas = []
        for data in groupData:
            if len(sqlUtil.retrieve_jsons(table='list_tel',condition='listId={}'.format(data['id']),show_sql=True))>0:
                continue
            saveData = {}
            reqUrl='https://99vv81.com'+data['url']
            seleniumUtils.open_url(URL=reqUrl,max_value=5)
            html=loadTel(seleniumUtils)
            saveData['id']=0
            saveData['listId']=data['id']
            saveData['html']=htmlUtil.to_str(htmlUtil.find_element(html=html,xpath_exp='//div[@class="info hide showContact"]'))
            saveDatas.append(saveData)
        print("开始准备保存第一批数据")
        if len(saveDatas)>0:
            sqlUtil.batch_insert(table='list_tel', data_list=saveDatas)
        print(f"完成保存第一批数据数量为:{len(saveDatas)}")
